<template>
  <view class="report">
    <scroll-view class="reportList" scroll-y @scrolltolower="loadMore">
      <reportItem v-for="(item, index) in reportList" :isPreview="true" :key="index" :reportItem="item" />
      <u-loadmore v-if="reportList.length || !loaded" class="list-more" :status="moreLoading" :load-text="loadText" />
      <Empty v-if="loadEnd && !reportList.length" :baseImg="emptyImg" text="暂无列表~" :fontWeight="400" />
    </scroll-view>
  </view>
</template>

<script>
import {
  reqGetChildByMajorId,
  reqGetIndexReportList,
  reqClickReport,
} from "@/api/modules/report";
import { appAddLinkOpenRecord } from "@/api/modules/preview";

import reportItem from "@/components/reportItem/index";

import Empty from "@/components/Empty.vue";

export default {
  components: {
    reportItem,
    Empty,
  },
  props: {
    options: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      srhIcon: this.$imgBase + "home/search.png",
      navHeight: 0,
      loading: false,
      status_bar: true,
      categoryList: [],
      reportList: [],
      option: { auto: true, page: 0, noMoreText: "已经到底啦" },
      sharePersonId: "",
      optionUrl: "",
      searchIcon: this.$imgBaseSelf + "/report/search.png",
      entranceSrc: this.$imgBaseSelf + "/report/ActivityEntrance.gif",
      emptyImg: this.$imgBaseSelf + "common/empty.png",
      moreLoading: "loadmore",
      loadText: {
        loadmore: "加载更多",
        loading: "加载中",
        nomore: "已经到底啦",
      },
      pageParams: {
        pageNo: 0,
        pageSize: 20,
      },
      loaded: false,
    };
  },
  computed: {
    personId() {
      return uni.getStorageSync("person-id").slice(2) || "";
    },
  },
  mounted() {
    let options = this.options;
    // 处理链接
    if (options.q) {
      let paramStr = decodeURIComponent(decodeURIComponent(options.q)).split(
        "?"
      );
      this.optionUrl = decodeURIComponent(decodeURIComponent(options.q));
      let resObj = {};
      if (paramStr[1]) {
        paramStr[1].split("&") &&
          paramStr[1].split("&").forEach((item) => {
            resObj[item.split("=")[0]] = item.split("=")[1];
          });
      }
      if (resObj.hasOwnProperty("sharePersonId")) {
        this.sharePersonId = resObj.sharePersonId;
      }
    } else {
      this.sharePersonId = options && options.sharePersonId;
    }
    // this.getCategoryList();
    this.appAddLinkOpenRecord();
    this.getData();
  },
  methods: {
    getCategoryList() {
      this.loading = true;
      let params = { majorIds: "0", typeLevel: 0 };
      reqGetChildByMajorId(params, this.personId)
        .then((res) => {
          this.categoryList = res.data.slice(0, 9);
          //添加一个更多
          this.categoryList.push({
            selfName: "全部分类",
            level: 1,
            isMore: true,
            iconUrl:
              "https://upload.yl1001.com/upload/images/view?file=p8YQ8FtiMeFxtY8wYalTMRF1F,R,VIS1TzoiUQTXNex,s,lOYwlNL7V3VK_gY.png",
          });
        })
        .catch((err) => {
          console.log("console.error();", err);
        })
        .finally((res) => {
          this.loading = false;
        });
    },
    toSearch() {
      uni.navigateTo({
        url: `/pagesSub/public/searchCareer/index`,
      });
    },
    handleBackClick() {
      uni.navigateBack();
    },
    refresh(paging) {
      const params = {
        pageNo: 0,
        pageSize: paging.size,
        personId: this.personId,
        moduleType: "OCCUPATION_MODULE_TYPE",
      };
      reqGetIndexReportList(params, this.personId)
        .then((res) => {
          this.reportList = res.data;
          this.$refs.list.refreshSuccess({
            list: this.reportList,
            total: res.pageparam.sums,
          });
        })
        .catch((e) => {
          this.$refs.list.refreshFail();
        });
    },
    getData(isMore) {
      // if(!this.personId) return;
      if (this.moreLoading == "loading" || this.moreLoading == "nomore") return;
      const params = {
        pageNo: this.pageParams.pageNo,
        pageSize: this.pageParams.pageSize,
        personId: this.personId,
        moduleType: "OCCUPATION_MODULE_TYPE",
      };
      this.moreLoading = "loading";
      reqGetIndexReportList(params, this.personId)
        .then((res) => {
          this.moreLoading = "loadmore";
          this.loaded = true;
          if (res && res.code == 200) {
            if (isMore) {
              this.reportList.push(...res.data);
            } else {
              this.reportList = res.data;
            }
            this.pageParams.pageNo++;
            if (this.pageParams.pageNo > res.pageparam.pages) {
              this.moreLoading = "nomore";
            }
          } else {
            this.moreLoading = "nomore";
            uni.showToast({
              title: res.status_desc,
              icon: "none",
            });
          }
        })
        .catch((e) => {
          this.moreLoading = "nomore";
        });
    },
    loadMore() {
      this.getData(true);
    },
    handleCategoryClick(item) {
      //点击分类埋点，更多按钮的情况发20
      let params = {
        personId: this.personId,
        pageType: item.isMore ? 20 : 30,
        platform: 20,
        moduleType: "OCCUPATION_MODULE_TYPE",
        majorId: item.isMore ? "" : item.selfId,
        majorName: item.isMore ? "" : item.selfName,
      };
      reqClickReport(params, this.personId);

      if (item.isMore) {
        uni.navigateTo({
          url: `/pagesSub/reportCate/allCategory/index`,
        });
        return;
      }
      uni.navigateTo({
        url: `/pagesSub/reportCate/careerCategory/index?majorId=${item.selfId}&categoryName=${item.selfName}`,
      });
    },
    handleSearchClick() {
      // uni.navigateTo({
      //   url: `/pages/searchCareer/index`,
      // });
    },
    // 数据埋点
    appAddLinkOpenRecord() {
      if (this.sharePersonId && this.personId) {
        let param = {
          mpaActivityId: "",
          activityName: "",
          activityDesc: "",
          sharePersonId: this.sharePersonId,
          openPersonId: this.personId,
          openPersonName: "",
          url: this.optionUrl,
          moduleType: "OCCUPATION_MODULE_TYPE",
        };
        appAddLinkOpenRecord(param, this.personId)
          .then((res) => {
            // console.log(res);
          })
          .finally(() => {
            // this.loading = false;
          });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.report {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  height: 100%;

  .reportList {
    height: 1px;
    flex-grow: 1;
  }
}
</style>
